﻿
/*################################################################ AUTHOR - SAVITHRI MANNAZHIMANA BABU ################################################################ */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentRepresentative
{
    public partial class ViewDisbursementList : System.Web.UI.Page
    {
        ViewDisbursementListControl disbursementControl;
        List<DisbursementCatalogue> disbursement_list;
        AssignStationeryItemsControl assignItemsControl;
        string temp_req_id;
        int reqformId;
        string empId;
        String depId;
        Requisition requisition;
        int disableCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                 if (!Page.IsPostBack)
                {
                    Employee currentuser = (Employee)Session["CurrentUser"];
                    empId = currentuser.EmpId;
                    //empId = "10040";
                    lblError.Text = "Latest approved Requisitions:";
                    btnCancel.Text = "Cancel";
                    disbursementControl = new ViewDisbursementListControl();
                    disbursement_list = new List<DisbursementCatalogue>();
                    depId = disbursementControl.getDeptId(empId);

                    disbursement_list = disbursementControl.getDisbursementListForDept(depId);

                    if (disbursement_list != null && disbursement_list.Count > 0)
                    {                       
                        btnCancel.Text = "Cancel";

                        gvDisbursementList.DataSource = disbursement_list;
                        gvDisbursementList.DataBind();

                        ViewState["DisbursementListView"] = disbursement_list;

                        int assignCount = (int)Session["AssignCount"];
                        if (assignCount == disbursement_list.Count)
                        {
                            btnSubmit.Enabled = true;
                        }
                    }
                    else
                    {                        
                        lblError.Text = "No approved Requisitions!";
                        btnCancel.Text = "Ok";
                    }

                     //check to enable Submit button
                    disableCount = (int)ViewState["disableCount"];
                    if (disableCount == disbursement_list.Count)
                    {
                        lblError.Text = "No approved Requisitions!";
                        btnSubmit.Enabled = true;
                    }
                }

            }
            catch (Exception Ex)
            {
                
            }
        }

        protected void gvDisbursementList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            lblError.Visible = false;
            if (e.CommandName == "btnAssignClick")
            {
                int rowindex = Convert.ToInt32(e.CommandArgument);              
                string itemName = gvDisbursementList.Rows[rowindex].Cells[3].Text;
                string receivedQty = gvDisbursementList.Rows[rowindex].Cells[5].Text;
                string itemId = gvDisbursementList.Rows[rowindex].Cells[1].Text;
                Session["itemName"] = itemName;
                Session["receivedQty"] = receivedQty;
                Session["itemId"] = itemId;
                
                Response.Redirect("AssignStationeryItems.aspx");
            }
        }

        protected void gvDisbursementList_SelectedIndexChanged(object sender, EventArgs e)
        {           
            

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //Response.Redirect("DepartmentRep.master");
        }

        protected void gvDisbursementList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDisbursementList.PageIndex = e.NewPageIndex;
            gvDisbursementList.DataSource = ViewState["DisbursementListView"];
            gvDisbursementList.DataBind();
        }

        protected void btnCancel_Click1(object sender, EventArgs e)
        {
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            disbursementControl = new ViewDisbursementListControl();
            Employee currentuser = (Employee)Session["CurrentUser"];
            empId = currentuser.EmpId;
            //empId = "10040";
            depId = disbursementControl.getDeptId(empId);
            assignItemsControl = new AssignStationeryItemsControl();
           List<Requisition> list = assignItemsControl.getRequisitionByDeptIdAndApprovedStatus(depId);
           foreach (Requisition req in list)
           {
               requisition = new Requisition();
               requisition.Status = "Assigned";
               assignItemsControl.updateRequisition(requisition, req.ReqFormId);
           }
        }

        
    }
} 